package com.wellsoft.mobileoa.utils.view;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListItem;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListSet;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FunctionViewAdapter extends BaseAdapter {
	public WSOA_PageListSet pageListSet;
	public BaseActivity baseActivity;
	private Resources resources;

	public FunctionViewAdapter(WSOA_PageListSet pageListSet,
			BaseActivity baseActivity) {
		this.pageListSet = pageListSet;
		this.baseActivity = baseActivity;

		resources = baseActivity.getResources();
	}

	@Override
	public int getCount() {
		if (pageListSet == null || pageListSet.getListItems() == null) {
			return 0;
		} else {
			return pageListSet.getListItems().size();
		}
	}

	@Override
	public Object getItem(int position) {
		return pageListSet.getListItems().get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(baseActivity).inflate(
				R.layout.functionviewitem, null);
		WSOA_PageListItem listItem = pageListSet.getListItems().get(position);

		TextView funcNameTv = (TextView) convertView
				.findViewById(R.id.functionviewitem_functv);
		ImageView funcIv = (ImageView) convertView
				.findViewById(R.id.functionviewitem_funciv);
		funcNameTv.setText(listItem.getMessage());

		int indentify = resources.getIdentifier(baseActivity.getPackageName()
				+ ":drawable/" + listItem.getImg(), null, null);
		if (indentify > 0) {
			funcIv.setImageResource(indentify);
		}
		return convertView;
	}
}
